package com.ybb.production.controller;

import com.ybb.production.domain.GarmentProductionOrder;
import com.ybb.production.dto.GarmentProductionCompleteDetailDTO;
import com.ybb.production.dto.GarmentProductionOrderDetailDTO;
import com.ybb.production.dto.GarmentProductionOrderQueryDTO;
import com.ybb.production.dto.GarmentProductionOrderUpsertDTO;
import com.ybb.production.responce.*;
import com.ybb.production.service.IGarmentProductionOrderService;
import com.yubb.common.annotation.Log;
import com.yubb.common.core.controller.BaseController;
import com.yubb.common.core.domain.AjaxResult;
import com.yubb.common.core.domain.R;
import com.yubb.common.core.page.TableDataInfo;
import com.yubb.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 生产工单Controller
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/production/order")
@Api(tags = "生产管理")
public class GarmentProductionOrderController extends BaseController {
    @Autowired
    private IGarmentProductionOrderService garmentProductionOrderService;

    /**
     * 查询生产工单列表
     */
    @RequiresPermissions("production:order:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询生产工单列表")
    public TableDataInfo<GarmentProductionOrderPageResp> list(GarmentProductionOrderQueryDTO garmentProductionOrder) {
        startPage();
        List<GarmentProductionOrderPageResp> list = garmentProductionOrderService.selectGarmentProductionOrderList(garmentProductionOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("production:order:list")
    @GetMapping("/getProductionOrderStatistics")
    @ApiOperation(value = "查询生产工单统计信息")
    public R<GarmentProductionOrderStaResp> getProductionOrderStatistics() {
        return R.ok(garmentProductionOrderService.getProductionOrderStatistics());
    }

    @RequiresPermissions("production:order:list")
    @GetMapping("/getProductionOrderDetailList/{orderId}")
    @ApiOperation(value = "查询生产工单列表裁床明细")
    public TableDataInfo<GarmentProductionOrderDetailResp> getProductionOrderDetailList(@PathVariable("orderId") Long orderId) {
        startPage();
        List<GarmentProductionOrderDetailResp> list = garmentProductionOrderService.getProductionOrderDetailList(orderId);
        return getDataTable(list);
    }


    /**
     * 获取生产工单详细信息
     */
    @RequiresPermissions("production:order:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取生产工单详细信息")
    public R<GarmentProductionOrderDetailDTO> getInfo(@PathVariable("id") Long id) {
        return R.ok(garmentProductionOrderService.selectProductionOrderDetail(id));
    }

    /**
     * 新增生产工单
     */
    @RequiresPermissions("production:order:add")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增生产工单")
    public R<GarmentProductionOrder> add(@RequestBody @Valid GarmentProductionOrderUpsertDTO garmentProductionOrder) {
        return R.ok(garmentProductionOrderService.insertGarmentProductionOrder(garmentProductionOrder));
    }

    /**
     * 修改生产工单
     */
    @RequiresPermissions("production:order:edit")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改生产工单")
    public AjaxResult edit(@RequestBody GarmentProductionOrderUpsertDTO garmentProductionOrder) {
        return toAjax(garmentProductionOrderService.updateGarmentProductionOrder(garmentProductionOrder));
    }

    @RequiresPermissions("production:order:edit")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editStatus")
    @ApiOperation(value = "修改生产工单状态")
    public AjaxResult editStatus(@RequestBody GarmentProductionOrderUpsertDTO garmentProductionOrder) {
        return toAjax(garmentProductionOrderService.updateOrderStatus(garmentProductionOrder));
    }

    /**
     * 删除生产工单
     */
    @RequiresPermissions("production:order:remove")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除生产工单")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(garmentProductionOrderService.deleteGarmentProductionOrderByIds(ids));
    }

    /**
     * 查看详情-详细列表
     */
    @RequiresPermissions("production:order:query")
    @GetMapping("/detailList/{orderId}")
    @ApiOperation(value = "查看详情-生产详情列表")
    public R<List> detailList(@PathVariable("orderId") Long orderId) {
        return R.ok(garmentProductionOrderService.detailList(orderId));
    }

    /**
     * 查看详情-生产进度（工序单价）- 每床明细
     */
    @RequiresPermissions("production:order:query")
    @GetMapping("/progressByBedNo/{orderId}")
    @ApiOperation(value = "查看详情-生产进度（工序单价）- 每床明细")
    public R<List<GarmentProductionProgressByBedNoResp>> progressByBedNo(@PathVariable("orderId") Long orderId) {
        return R.ok(garmentProductionOrderService.progressByBedNo(orderId));
    }

    /**
     * 查看详情-生产进度（工序单价）- 每床明细下拉列表
     */
    @RequiresPermissions("production:order:query")
    @GetMapping("/completeDetail")
    @ApiOperation(value = "查看详情-生产进度（工序单价）- 每床明细下拉列表")
    public R<List<GarmentBianfeiCompleteDetailResp>> completeDetail(GarmentProductionCompleteDetailDTO dto) {
        return R.ok(garmentProductionOrderService.completeDetail(dto));
    }

    /**
     * 查看详情-生产进度（工序单价）- 合并统计
     */
    @RequiresPermissions("production:order:query")
    @GetMapping("/combinedStatistic/{orderId}")
    @ApiOperation(value = "查看详情-生产进度（工序单价）- 合并统计")
    public R<List<GarmentProductionProgressByBedNoResp.CuttingBedProcess>> combinedStatistics(@PathVariable Long orderId) {
        return R.ok(garmentProductionOrderService.combinedStatistics(orderId));
    }
}
